//名单导入
/**
 * @openapi
 * tags:
 * - name: name_list
 *   description: 名单
 */
const router = require("express-promise-router")();

const path=require('path');
const multer  = require('multer');
const config=require("../../config/config");
const template_file = config.template_file;
const tmp_uploads_dir=config.tmp_uploads_dir; //存放临时上传的文件。导入完成后就可以删除。不做hash处理
const upload = multer({ dest: tmp_uploads_dir });
//console.log('tmp_uploads_dir',tmp_uploads_dir);


const NameListsService = require("../../services/name_lists");
const LogService = require("../../services/log");

/**
 * @openapi
 * /teacher/name_lists/import/check_file:
 *   post:
 *     tags:
 *     - name_list
 *     summary: 导入成员第一步，检查excel文件
 *     requestBody:
 *       content:
 *         multipart/form-data:
 *           schema:
 *             type: object
 *             properties:
 *               file:
 *                 type: string
 *                 format: binary
 *               nameRowNumber:
 *                 type: number
 *               firstDataRowNumber:
 *                 type: number
 *               fieldsMapping:
 *                 type: object  例如："{\"学号\":\"username\",\"姓名\":\"showName\",\"专业\":\"subject\",\"班级\":\"eduClass\"}"
 *     responses:
 *       200:
 *         description: 返回JSON文件信息
 */
//从excel文件导入用户，可传递标题行与数据开始行，可以传递一个表头映射对象
router.post('/import/check_file', upload.single('file'), async function  (req, res){
    // req.file 是 `users` 文件的信息
    // req.body 将具有文本域数据，如果存在的话
    //req.file.path 是文件路径
    var default_nameRowNumber = 1;
    var default_firstDataRowNumber = 2;
    if(template_file==='users_kmust.xlsx'){
        default_nameRowNumber = 5;
        default_firstDataRowNumber = 6;
    }
    let {nameRowNumber=default_nameRowNumber,firstDataRowNumber=default_firstDataRowNumber,fieldsMapping}=req.body;
    if(fieldsMapping){
        fieldsMapping=JSON.parse(fieldsMapping);
    }
    //console.log("fieldsMapping",fieldsMapping);
    //检查Excel文件是否合乎格式
    const result=await NameListsService.checkExcelFile(req.file.path,nameRowNumber,firstDataRowNumber,fieldsMapping);
    if(result.error){
        return res.status(400).send({"error":result.error});
    }else{
        return res.json(result);
    }
});

/*
{
  "filePath": "C:\\x_项目\\lims_v5\\tmp_uploads\\5ab1bed9b851ff3be2eec9cfc7023b73",
  "nameRowNumber": 6,
  "firstDataRowNumber": 7,
  "fieldsMapping": {"学号":"username"}
}
* */
/**
 * @openapi
 * /teacher/name_lists/import/confirm_file:
 *   post:
 *     tags:
 *     - name_list
 *     summary: 导入成员第二步，确认导入excel文件
 *     requestBody:
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               filePath:
 *                 type: string
 *               nameRowNumber:
 *                 type: number
 *               firstDataRowNumber:
 *                 type: number
 *               fieldsMapping:
 *                 type: object
 *               id:
 *                 type: string
 *       required: true
 *     responses:
 *       200:
 *         description: 返回有几条重复，几条新增
 */
router.post('/import/confirm_file',  async function  (req, res){
    //从客户端取得要导入的文件路径，由check_file给出
    var default_nameRowNumber = 1;
    var default_firstDataRowNumber = 2;
    if(template_file==='users_kmust.xlsx'){
        default_nameRowNumber = 5;
        default_firstDataRowNumber = 6;
    }
    let {nameRowNumber=default_nameRowNumber,firstDataRowNumber=default_firstDataRowNumber,filePath,fieldsMapping,owner}=req.body;
    // if(fieldsMapping){
    //     fieldsMapping=JSON.parse(fieldsMapping);
    // }
    //导入到数据库
    const result=await NameListsService.importExcelFile(filePath,nameRowNumber,firstDataRowNumber,fieldsMapping,owner);
    LogService.addLog(req.session.user,"名单",`导入批量名单`);
    res.json(result);
});
module.exports = router;